﻿using System;
using System.Collections.Generic;
using Csla;
using Ent.AccesoDatos;

namespace Ent.Inscripcion.Core.Core
{
    [Serializable]
    public class ValoresEstadisticaLista :
      ReadOnlyListBase<ValoresEstadisticaLista, ValorEstadisticaInfo>
    {

        #region Factory Methods

        public static ValoresEstadisticaLista GetValoresEstadisticaLista(ValorEstadisticaCriterio criteria)
        {
            return DataPortal.FetchChild<ValoresEstadisticaLista>(criteria);
        }

        private ValoresEstadisticaLista()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(ValorEstadisticaCriterio criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            // TODO: load values
            var accesoDatos = new DriveDataBase(TypeDataBase.Mysql);
            var parametros = new List<Parametro> { new Parametro("Variable", criteria.Variable) };
            var nombreS = criteria.TipoReporte == Tipo.Tutoria ? "ValoresReporteEstadisticoTutoria" : "ValoresReporteEstadisticoExamen";
            var datos = accesoDatos.EjecutarReader(nombreS, parametros);
            while (datos.Datos.Read())
            {
                Add(ValorEstadisticaInfo.GetValorEstadisticaInfo(datos));
            }
            if (!datos.Datos.IsClosed) datos.Datos.Close();
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class ValorEstadisticaInfo : ReadOnlyBase<ValorEstadisticaInfo>
    {
        #region Business Methods

        private static PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(o => o.Id, "Name");
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
        }

        public static readonly PropertyInfo<string> ValorProperty = RegisterProperty<string>(c => c.Valor);
        /// <Summary>
        /// Gets or sets the Valor value.
        /// </Summary>
        public string Valor
        {
            get { return GetProperty(ValorProperty); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombres);
        /// <Summary>
        /// Gets or sets the Nombre value.
        /// </Summary>
        public string Nombres
        {
            get { return GetProperty(NombreProperty); }
        }

        public static readonly PropertyInfo<string> IdentificacionProperty = RegisterProperty<string>(c => c.Identificacion);
        /// <Summary>
        /// Gets or sets the Identificacion value.
        /// </Summary>
        public string Identificacion
        {
            get { return GetProperty(IdentificacionProperty); }
        }

        public static readonly PropertyInfo<string> EmailProperty = RegisterProperty<string>(c => c.Email);
        /// <Summary>
        /// Gets or sets the Email value.
        /// </Summary>
        public string Email
        {
            get { return GetProperty(EmailProperty); }
        }

        public static readonly PropertyInfo<string> PeriodoProperty = RegisterProperty<string>(c => c.Periodo);
        /// <Summary>
        /// Gets or sets the Periodo value.
        /// </Summary>
        public string Periodo
        {
            get { return GetProperty(PeriodoProperty); }
        }

        private static PropertyInfo<string> DiaProperty = RegisterProperty<string>(o => o.Dia, "Name");
        public string Dia
        {
            get { return GetProperty(DiaProperty); }
        }

        private static PropertyInfo<string> NivelProperty = RegisterProperty<string>(o => o.Nivel, "Name");
        public string Nivel
        {
            get { return GetProperty(NivelProperty); }
        }

        private static PropertyInfo<string> CarreraProperty = RegisterProperty<string>(o => o.Carrera, "Name");
        public string Carrera
        {
            get { return GetProperty(CarreraProperty); }
        }

        private static PropertyInfo<DateTime> FechaProperty = RegisterProperty<DateTime>(o => o.Fecha, "Name");
        public DateTime Fecha
        {
            get { return GetProperty(FechaProperty); }
        }

        private static PropertyInfo<DateTime> HoraInicioProperty = RegisterProperty<DateTime>(o => o.HoraInicio, "Hora Inicio");
        public DateTime HoraInicio
        {
            get { return GetProperty(HoraInicioProperty); }
        }

        private static PropertyInfo<DateTime> HoraFinProperty = RegisterProperty<DateTime>(o => o.HoraFin, "Hora Fin");
        public DateTime HoraFin
        {
            get { return GetProperty(HoraFinProperty); }
        }

        #endregion

        #region Factory Methods

        internal static ValorEstadisticaInfo GetValorEstadisticaInfo(object childData)
        {
            return DataPortal.FetchChild<ValorEstadisticaInfo>(childData);
        }

        private ValorEstadisticaInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(Reader datos)
        {
            LoadProperty(IdProperty, Guid.NewGuid());
            LoadProperty(ValorProperty, datos.Datos.GetString(1));
            LoadProperty(IdentificacionProperty, datos.Datos.GetString(3));
            LoadProperty(NombreProperty, string.Format("{0} {1}", datos.Datos.GetString(4), datos.Datos.GetString(5)));
            LoadProperty(FechaProperty, DateTime.Parse(datos.Datos.GetString(6)));
            LoadProperty(HoraInicioProperty, DateTime.Parse(datos.Datos.GetString(7)));
            LoadProperty(HoraFinProperty, DateTime.Parse(datos.Datos.GetString(8)));
            LoadProperty(CarreraProperty, datos.Datos.GetString(9));
            LoadProperty(PeriodoProperty, datos.Datos.GetString(11));
            LoadProperty(NivelProperty, datos.Datos.GetString(13));
            LoadProperty(DiaProperty, datos.Datos.GetString(16));
        }

        #endregion
    }
}